{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def euclidean_distance(x, y):\n",
    "    if len(x) == len(y):\n",
    "        return (np.sqrt(np.sum(np.power((x - y), 2))))\n",
    "    else:\n",
    "        print('Input should be of equal length')\n",
    "        return (None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def IrNorm_distance(x, y, power):\n",
    "    if len(x) == len(y):\n",
    "        return (np.power(np.sum(np.power(np.abs(x - y), power)),\n",
    "                         (1 / (1.0 * power))))\n",
    "    else:\n",
    "        print('Input Input should be of equal length')\n",
    "        return (None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cosine_distance(x, y):\n",
    "    if len(x) == len(y):\n",
    "        return (np.dot(x, y) / np.sqrt(np.dot(x, x) * np.dot(y, y)))\n",
    "    else:\n",
    "        print('Input Input should be of equal length')\n",
    "        return (None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def jaccard_distance(x, y):\n",
    "    set_x = set(x)\n",
    "    set_y = set(y)\n",
    "    return (1 - len(set_x.intersection(set_y)) / len(set_x.union(set_y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hamming_distance(x, y):\n",
    "    diff = 0\n",
    "    if len(x) == len(y):\n",
    "        for char1, char2 in zip(x, y):\n",
    "            if char1 != char2:\n",
    "                diff += 1\n",
    "        return (diff)\n",
    "    else:\n",
    "        print('Input Input should be of equal length')\n",
    "        return (None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "0.0\n",
      "cosine distance:\n",
      "0.7071067811865475\n",
      "jaccard_distance:\n",
      "0.0\n",
      "hamming_distance:\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    x = np.asarray([1, 2, 3])\n",
    "    y = np.asarray([1, 2, 3])\n",
    "    print(euclidean_distance(x, y))\n",
    "    print(IrNorm_distance(x, y, 2))\n",
    "    x = [1, 0]\n",
    "    y = [1, 1]\n",
    "    print('cosine distance:')\n",
    "    print(cosine_distance(x, y))\n",
    "    x = [1, 2, 3]\n",
    "    y = [1, 2, 3]\n",
    "    print('jaccard_distance:')\n",
    "    print(jaccard_distance(x, y))\n",
    "    x = [11001]\n",
    "    y = [10110]\n",
    "    print('hamming_distance:')\n",
    "    print(hamming_distance(x, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
